//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action.web;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.mingsoft.ueditor.MsUeditorActionEnter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.exception.BusinessException;
import net.mingsoft.base.util.BundleUtil;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@Controller("ueAction")
@RequestMapping({"/static/plugins/ueditor/{version}/jsp"})
public class EditorAction {
    public EditorAction() {
    }

    @ResponseBody
    @RequestMapping(
            value = {"editor"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String editor(HttpServletRequest request, HttpServletResponse response, String jsonConfig) {
        String uploadFolderPath = MSProperties.upload.path;
        boolean enableWeb = MSProperties.upload.enableWeb;
        if (!enableWeb) {
            HashMap<String, String> map = new HashMap();
            map.put("state", "front end upload is not enabled");
            return JSONUtil.toJsonStr(map);
        } else {
            String rootPath = BasicUtil.getRealPath(uploadFolderPath);
            jsonConfig = jsonConfig.replace("{ms.upload}", "/" + uploadFolderPath);
            Map<String, Object> map = (Map)JSONUtil.toBean(jsonConfig, Map.class);
            String imagePathFormat = (String)map.get("imagePathFormat");
            imagePathFormat = FileUtil.normalize(imagePathFormat);
            String filePathFormat = (String)map.get("filePathFormat");
            filePathFormat = FileUtil.normalize(filePathFormat);
            String videoPathFormat = (String)map.get("videoPathFormat");
            videoPathFormat = FileUtil.normalize(videoPathFormat);
            map.put("imagePathFormat", imagePathFormat);
            map.put("filePathFormat", filePathFormat);
            map.put("videoPathFormat", videoPathFormat);
            jsonConfig = JSONUtil.toJsonStr(map);
            if (jsonConfig == null || !jsonConfig.contains("../") && !jsonConfig.contains("..\\")) {
                MsUeditorActionEnter actionEnter = new MsUeditorActionEnter(request, rootPath, jsonConfig, BasicUtil.getRealPath(""));
                String json = actionEnter.exec();
                Map jsonMap = (Map)JSONUtil.toBean(json, Map.class);
                jsonMap.put("url", "/".concat(uploadFolderPath).concat(jsonMap.get("url") + ""));
                return JSONUtil.toJsonStr(jsonMap);
            } else {
                throw new BusinessException(BundleUtil.getString("net.mingsoft.base.resources.resources", "err.error", new String[]{BundleUtil.getString("net.mingsoft.basic.resources.resources", "file.path", new String[0])}));
            }
        }
    }
}
